//
//  U9EncryptManager.m
//  U9WatchGame
//
//  Created by uuu9 on 2017/8/8.
//  Copyright © 2017年 UUU9. All rights reserved.
//

#import "U9EncryptManager.h"
#import <CommonCrypto/CommonDigest.h>

@implementation U9EncryptManager



#pragma mark ----------------------------------------  工具方法
#pragma mark 密码加密
+ (NSString *)convertPassword:(NSString *)passwd{
    NSString *newKey = [kSecretKey substringToIndex:passwd.length];
    NSData* keyData = [newKey dataUsingEncoding:NSUTF8StringEncoding];
    NSData* passwdData = [passwd dataUsingEncoding:NSUTF8StringEncoding];
    Byte *keyBytes = (Byte *)[keyData bytes];
    Byte *passwdBytes = (Byte *)[passwdData bytes];
    NSMutableString *hexStr = [NSMutableString string];
    for(int i = 0;i < [keyData length]; i++)
    {
        Byte newByte = keyBytes[i] ^ passwdBytes[i];
        NSString *newHexStr = [NSString stringWithFormat:@"%x",newByte&0xff];///16进制数
        if([newHexStr length]==2){
            [hexStr appendString:newHexStr];
        }
        else{
            [hexStr appendFormat:@"0%@", newHexStr];
        }
    }
    ;
    return [self md5:[hexStr uppercaseString]];
}

#pragma mark md5加密
+ (NSString *)md5:(NSString *)str
{
    const char *cStr = [str UTF8String];
    unsigned char result[16];
    CC_MD5(cStr, strlen(cStr), result); // This is the md5 call
    NSString *md5Str = [NSString stringWithFormat:
                        @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
                        result[0], result[1], result[2], result[3],
                        result[4], result[5], result[6], result[7],
                        result[8], result[9], result[10], result[11],
                        result[12], result[13], result[14], result[15]
                        ];
    return [md5Str uppercaseString];
}

#pragma mark base64加密
+ (NSString *)base64Encoding:(NSString *)encoingStr{
    NSData *data = [encoingStr dataUsingEncoding:NSUTF8StringEncoding];
    NSString *baseStr = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
    return baseStr;
}

+ (NSString *)base64Decoding:(NSString *)decodingStr{
    NSData *nsdataFromBase64String = [[NSData alloc] initWithBase64EncodedString:decodingStr options:0];
    NSString *base64Decoded = [[NSString alloc] initWithData:nsdataFromBase64String encoding:NSUTF8StringEncoding];
    return base64Decoded;
}



@end
